<template>
  <div class="treecheck">
    <div class="buttn">
      <el-button type="primary" size="small" @click="dialogVisible=true"><i class="el-icon-plus" ></i> 新增分组</el-button>
      <el-button type="primary" size="small" @click="xiugai">修改</el-button>
      <el-button type="danger" size="small" @click="deletelist">删除</el-button>
    </div>
    <div>
      <el-tree ref="tree" node-key="groupCode" :data="data" :props="defaultProps" @node-click="handleNodeClick" :highlight-current="true"></el-tree>
    </div>
    <div class="dialog">
      <el-dialog
        title="分组信息"
        :visible.sync="dialogVisible"
        width="25%">
        <div>
          <el-form ref="form" :model="form" label-width="100px">
            <el-col :span="24">
              <el-form-item label="分组名称">
                <el-input v-model="form.groupName" size="small" placeholder="请输入分组名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分组编码">
                <el-input v-model="form.groupCode" :disabled="isflag" size="small" placeholder="请输入分组编码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分组类型">
                <el-select v-model="form.groupType" placeholder="请选择分组类型" size="small" style="width: 100%;">
                  <el-option label="数据完整性检查" value="1"></el-option>
                  <el-option label="数据异常分析" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="排序号">
                <el-input v-model="form.groupOrder" size="small" placeholder="请输入排序号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分组状态">
                <el-radio-group v-model="form.groupStatus">
                  <el-radio label="1">启用</el-radio>
                  <el-radio label="0">停用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="quxiao" size="small">取 消</el-button>
          <el-button type="primary" @click="submit" size="small">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {getfenzulistapi,postaddenzuapi,postupdatefenzuapi,postdelfenzuapi} from '@/api/system/CheckRules'
export default {
  name: 'CallPoliceTreeBox',

  data() {
    return {
      data:[],
      defaultProps: {
        label:'groupName'
      },
      dialogVisible: false,
      form: {groupStatus:'0'},
      options: [],
      treevalue: {},
      isflag:false
    };
  },

  async mounted() {
    await this.gettreelist()
    this.$refs.tree.setCurrentKey(this.data[0].groupCode);
    this.handleNodeClick(this.data[0])
  },

  methods: {
    // 修改/新增分钟
    async submit() {
      this.isflag ? postupdatefenzuapi(this.form).then(res => {
        this.$message({
          type:res.code=='200'?'success':'warning',
          message:res.code=='200'?'分组编辑成功':res.message
        })
        if(res.code=='200') this.shuaxinshu()
      }):postaddenzuapi(this.form).then(res => {
        this.$message({
          type:res.code=='200'?'success':'warning',
          message:res.code=='200'?'分组新增成功':res.message
        })
        if(res.code=='200') this.shuaxinshu()
      })
      this.quxiao()
    },
    // 树节点点击
    handleNodeClick(val) {
      this.$emit('handleNodeClick',val,this.data)
      this.treevalue=val
    },
    // 弹窗取消
    quxiao() {
      this.dialogVisible = false
    },
    // 获取树数据
    async gettreelist() {
      const { data: { list } } = await getfenzulistapi()
      this.data=list
    },
    // 删除分组
    deletelist() {
      this.$confirm('此操作将删除选中分组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          postdelfenzuapi(this.treevalue.groupCode).then(res => {
            this.$message({
              type:res.code=='200'?'success':'warning',
              message:res.code=='200'?'删除成功':res.message
            })
            if(res.code=='200') this.shuaxinshu()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑
    xiugai() {
      if (!this.treevalue.groupCode) return this.$message.warning('请先选择分组信息')
      this.form = { ...this.treevalue }
      this.isflag=true
      this.dialogVisible = true
    },
    // 刷新树
    async shuaxinshu() {
      await this.gettreelist()
      if (!this.data.some(ele=>ele.groupCode==this.treevalue.groupCode)) {
        this.$emit('deletelist')
      } else {
        this.$refs.tree.setCurrentKey(this.treevalue.groupCode)
        this.treevalue=this.data.find(ele=>ele.groupCode==this.treevalue.groupCode)
        this.handleNodeClick(this.treevalue)
      }
    }
  },
  watch:{
    dialogVisible(val) {
      if (!val) {
        this.form = { groupStatus: '0' }
        this.isflag=false
      }
    }
  }
};
</script>

<style scoped>
.treecheck{
  width: 100%;
  height: 100%;
  .buttn{
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}
</style>